package ui.activity.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.CheckRegisterBean;
import bean.LoginBean;
import bean.RegisterBean;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.SPUtils;

public class RegisterActivity extends BaseNetActivity{
    private EditText ed_phone,
                    ed_smscode,
                    ed_passwd,
                    ed_inivet;
    private Button bt_send,
                   bt_next;
    private CheckBox check_paswd,
                    check_argee;
    private TextView tv_xieyi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("注册");
    }
    @Override
    public void success(String action, BaseBean baseBean) {
        if(action.equals("common/checkRegister")){
            CheckRegisterBean Bean = (CheckRegisterBean) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data),CheckRegisterBean.class);
            baseBean.data.toString();
            Log.i("data",baseBean.data.toString());
            if(baseBean.code == 200){
                if(!Bean.register){
                    Map<String,String> map = new HashMap<>();
                    map.put("mobile",ed_phone.getText().toString().trim());
                    map.put("code",ed_smscode.getText().toString().trim());
                    map.put("password",ed_passwd.getText().toString().trim());
                    map.put("invcode",ed_inivet.getText().toString().trim());
                    post("common/register",map, RegisterBean.class,true);
                }else {
                    toast("该手机号已被注册");
                }
            }else {
                toast(baseBean.msg);
            }
        }
        else if(action.equals("common/register")){
            if(baseBean.code==200){
                RegisterBean resultBean = new Gson().fromJson(baseBean.data.toString(),RegisterBean.class);
                SPUtils.getInstance(context).put("token",resultBean.token);
                SPUtils.getInstance(this).put("student_id",resultBean.student_id);

                SPUtils.getInstance(this).put("user_phone",ed_phone.getText().toString().trim());
                SPUtils.getInstance(this).put("password",ed_passwd.getText().toString().trim());
                Bundle bundle = new Bundle();
                bundle.putInt("student_id",resultBean.student_id);
                //bundle.putString("school_id",""+resultBean.school_id);
                bundle.putString("invcode",""+ed_inivet.getText().toString());
                bundle.putSerializable("Grade_info",resultBean.grade_info);
                bundle.putSerializable("School_info",resultBean.school_info);
                jumpTo(RegisterNextActivity.class,bundle);
                //finish();
                toast(baseBean.msg);
            }else {
                toast(baseBean.msg);
            }
        }else if(action.equals("common/sendVerCode")){
            if(baseBean.code == 200){
                /** 倒计时60秒，一次1秒 */
                CountDownTimer timer = new CountDownTimer(60*1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        // TODO Auto-generated method stub
                        bt_send.setText(millisUntilFinished/1000+"秒");
                    }

                    @Override
                    public void onFinish() {
                        bt_send.setText("获取验证码");
                    }
                }.start();
            }else {
                toast(baseBean.msg);
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void findViews() {
        ed_phone = getView(R.id.ed_user);
        ed_smscode = getView(R.id.ed_smscode);
        ed_passwd = getView(R.id.ed_password);
        ed_inivet = getView(R.id.ed_invitecode);
        bt_send = getView(R.id.bt_smscode);
        bt_next = getView(R.id.bt_next);
        check_paswd = getView(R.id.checkbox_showpassword);
        check_argee = getView(R.id.checkbox_agree);
        tv_xieyi = getView(R.id.tv_xieyi);
    }

    @Override
    protected void formatViews() {
        bt_next.setOnClickListener(this);
        bt_send.setOnClickListener(this);
        tv_xieyi.setOnClickListener(this);
        //显示密码
        check_paswd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    ed_passwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    ed_passwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT );
                }
            }
        });
        //阅读同意协议
        check_argee.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()) {
                    bt_next.setBackgroundResource(R.drawable.bt_click);
                    bt_next.setEnabled(true);
                }
                else {
                    bt_next.setBackgroundResource(R.color.gray);
                    bt_next.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected void formatData() {

    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //发送验证码
            case R.id.bt_smscode:
                Map<String,String> mapcode = new HashMap<>();
                mapcode.put("mobile",ed_phone.getText().toString().trim());
                mapcode.put("type","register");
                post("common/sendVerCode",mapcode, LoginBean.class,true);
                break;
            //下一步
            case R.id.bt_next:
                Map<String,String> map = new HashMap<>();
                map.put("mobile",""+ed_phone.getText().toString().trim());
                post("common/checkRegister",map, CheckRegisterBean.class,false);
                break;
            case R.id.tv_xieyi:
                jumpTo(ServiceAgreementActivity.class);
                break;
        }
    }
}
